<?php
/**
 * @title 443. 压缩字符串 - 指针
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[] $chars
     * @return Integer
     */
    function compress(&$chars) {
        /**
         * @since 2021-08-21 长度
         */
        $len = count($chars);

        /**
         * @since 2021-08-21 上个字符
         */
        $lastChar = $chars[0];
        $num = 1;
        $j = 1;

        /**
         * @since 2021-08-21 循环
         */
        for ($i=1; $i<$len; $i++){
            if($chars[$i] == $lastChar){
                $num++;
            } else {
                /**
                 * @since 2021-08-21 数量入队列
                 */
                if($num >= 2){
                    $num = strval($num);
                    for($n=0; $n<strlen($num); $n++){
                        $chars[$j] = $num[$n];
                        $j++;
                    }
                } else {}

                /**
                 * @since 2021-08-21 新的开始
                 */
                $chars[$j] = $chars[$i];
                $j++;
                $num = 1;
                $lastChar = $chars[$i];
            }
        }

        /**
         * @since 2021-08-21 收官
         */
        if($num >= 2){
            $num = strval($num);
            for($n=0; $n<strlen($num); $n++){
                $chars[$j] = $num[$n];
                $j++;
            }
        } else {}

        /**
         * @since 2021-08-21 去掉多余字符
         */
        if($j < $len){
            array_splice($chars, $j);
            // $chars = array_slice($chars, 0, $j);
        } else {}

        /**
         * @return
         */
        // return $chars;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202108/2021.08.21.php
 */
$datas = [
    [["a","a","b","b","c","c","c","a","a"]],
    // [["a","a","b","b","c","c","c"]], // ["a","2","b","2","c","3"]
    // [["a"]], // ["a"]
    // [["a","b","b","b","b","b","b","b","b","b","b","b","b"]], // ["a","b","1","2"]
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->compress(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
